package textprocessing.datasources.enwikipedia;

import com.carrotsearch.hppc.ObjectByteMap;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import textprocessing.nlpanalysis.context_annotate.ContextResult;
import textprocessing.nlpanalysis.context_annotate.ParagraphSplitor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by julianzliu on 5/6/2017.
 */
public class TransformerByInput {


    static final Logger logger = LoggerFactory.getLogger(TransformerByInput.class);


    private TransformerEnWikiNews transformer;
    private ExtractorByQuery extractor;
    private IndexerEnWikiNews indexer;
    private ObjectMapper mapper;

    public TransformerByInput(){
        this.transformer = new TransformerEnWikiNews();
        this.extractor = new ExtractorByQuery();
        this.indexer = new IndexerEnWikiNews();
        this.mapper = new ObjectMapper();
    }


    public void processInput(String para, String id){
        ObjectNode node = this.mapper.createObjectNode();
        node.put("text", para);
        if(id != null)
            node.put("id", id);
        else
            node.put("id", "-1");
        processPara( node );
    }



    private void processPara(JsonNode record){
        String id = record.get("id").asText();
        processSinglePara(record, id );
        logger.info("[processRecord] id = {}", id);
        System.out.println(String.format("\n\n[processRecord] id = %s", id));
    }

    private void processSinglePara(JsonNode documentNode, String id){
        String PARA = documentNode.get("text").asText();

        //List<String> doc_entities = new ArrayList<>();
        //List<String> doc_conetexs = new ArrayList<>();


        String para_id = this.transformer.loadParagraph( PARA , id);
        List<ContextResult> contexts = this.transformer.generateContext(PARA);
        for(ContextResult context : contexts){
            ObjectNode contextNode = this.transformer.genObjectNode(context, id, para_id);
            String context_id = this.transformer.loadContext( contextNode );

            logger.info("[processSinglePara] saved context = {}", context_id);
            //doc_conetexs.add(context_id);
            //doc_entities.addAll( context.entites );
        }

        /*
        目前不修改 doc_id 对应的 doc
         */


        /*
        ArrayNode doc_contexts_arr = this.mapper.createArrayNode();
        for(String context_id : doc_conetexs) doc_contexts_arr.add(context_id);
        ArrayNode doc_entities_arr = this.mapper.createArrayNode();
        for(String entity_id : doc_entities) doc_entities_arr.add(entity_id);
        */

        //((ObjectNode)documentNode).put("contexts", doc_contexts_arr);
        //((ObjectNode)documentNode).put("entities", doc_entities_arr);

        //this.transformer.loadDocument((ObjectNode)documentNode, id);
    }


    public JsonNode processText(String text){

        ObjectNode ret = this.mapper.createObjectNode();

        String PARA = text;

        List<ContextResult> contexts = this.transformer.generateContext(PARA);
        String id = "doc_id";
        String para_id = "para_id";

        Integer index = 0;

        for(ContextResult context : contexts){
            ObjectNode contextNode = this.transformer.genObjectNode(context, id, para_id);

            ret.put(index.toString(), contextNode);

            logger.info("[processText] saved context = {}", index);

            index += 1;
        }

        return ret;
    }



    /*********************************************
     *
     * @param args
     */
    public static void main(String[] args){
        TransformerByInput transformer = new TransformerByInput();
        transformer.processInput( "Craig Malcolm Robinson is elder brother of Michelle Obama.", "10973685" );
    }

}
